package com.ruoyi.act.service.impl;

import com.ruoyi.act.domain.QUERY.ActTaskFormQY;
import com.ruoyi.act.domain.QUERY.ActTaskStartQY;
import com.ruoyi.act.domain.VO.FormDataVO;
import com.ruoyi.act.domain.VO.HisNodeVo;
import com.ruoyi.act.domain.VO.TaskFormDataVo;
import com.ruoyi.act.service.ActTaskService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import org.activiti.engine.*;
import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ActTaskServiceImpl implements ActTaskService {
    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;
    @Autowired
    private RuntimeService runtimeService;

    /**
     * 启动流程实例
     * @param taskStartQUERY
     * @return
     */
    public AjaxResult start(ActTaskStartQY taskStartQUERY){
        Map<String, Object> variables = new HashMap<String,Object>();
        List<String> form_label = new ArrayList<>();
        for(ActTaskFormQY formData:taskStartQUERY.getFormData()){
            variables.put(formData.getLabel(), formData.getValue());
            form_label.add(formData.getLabel());
        }
        variables.put("task_id", taskStartQUERY.getId());
        variables.put("form_label", String.join(",",form_label));
        variables.put("start_user", SecurityUtils.getUsername());
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(taskStartQUERY.getId(), variables);
        runtimeService.setVariables(processInstance.getId(),variables);
        return AjaxResult.success();
    }

    /**
     * 获取任务
     * @param userName
     * @return
     */
    public AjaxResult tasks(String userName){
        List<Task> Tasklist = taskService.createTaskQuery().taskAssignee(userName).list();
        ArrayList<TaskFormDataVo> taskFormDataList = new ArrayList<>();
        for(Task task:Tasklist){
            String[] form_label = taskService.getVariable(task.getId(),"form_label",String.class).split(",");
            ArrayList<FormDataVO> formDataVOList = new ArrayList<>();
            for(String label:form_label){
                FormDataVO formDataVO = new FormDataVO();
                formDataVO.setLabel(label);
                formDataVO.setValue(taskService.getVariable(task.getId(),label,String.class));
                formDataVOList.add(formDataVO);
            }
            TaskFormDataVo taskFormDataVo = new TaskFormDataVo();
            taskFormDataVo.setTaskId(task.getId());
            taskFormDataVo.setFormData(formDataVOList);
            taskFormDataVo.setId(taskService.getVariable(task.getId(),"task_id",String.class));
            taskFormDataVo.setUserName(taskService.getVariable(task.getId(),"start_user",String.class));
            taskFormDataList.add(taskFormDataVo);
        }
        return AjaxResult.success(taskFormDataList);
    }

    /**
     * 完成任务
     * @param id 任务id
     * @return
     */
    public AjaxResult completeTask(String id) {
        taskService.complete(id);
        return AjaxResult.success();
    }

    public AjaxResult getHistoryTask(String id) {
        Task task = taskService.createTaskQuery().taskId(id).singleResult();
        List<HistoricTaskInstance> hisQuery = historyService.createHistoricTaskInstanceQuery().executionId(task.getExecutionId()).list();
        return AjaxResult.success(hisQuery);
    }
}
